package com.ezjie.ielts.module_word;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnGroupExpandListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.ezjie.ielts.R;
import com.ezjie.ielts.application.YsApplication;
import com.ezjie.ielts.core.base.BaseFragmentActivity;
import com.ezjie.ielts.model.ReviewGroupBean;
import com.ezjie.ielts.model.WordBean;
import com.ezjie.ielts.model.WordGroup;
import com.ezjie.ielts.model.WordGroupBean;
import com.ezjie.ielts.module_word.adapter.ReviewFinishAdapter;
import com.ezjie.ielts.util.PreferencesUtil;
import com.ezjie.ielts.util.UmengPages;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.umeng.analytics.MobclickAgent;

@ContentView(R.layout.layout_review_finish)
public class ReviewFinishActivity extends BaseFragmentActivity {

	private Context mContext;
	@ViewInject(R.id.navi_title_text)
	private TextView review_title;
	@ViewInject(R.id.choose_review)
	private TextView choose_review;
	@ViewInject(R.id.navi_back_btn)
	private ImageView backBtn;// 返回按钮
	@ViewInject(R.id.review_finish_rl)
	private RelativeLayout review_finish_rl;
	@ViewInject(R.id.review_list)
	private ExpandableListView review_list;
	// private Button next_learn;//下一组复习
	private int wgid;
	private int isReview;
	private boolean isFinish;

	private ReviewGroupBean mReviewGroupBean;
	private List<WordGroup> mGroups;// 所有的复习组
	private List<WordGroup> mReviewGroups;// 所有的需要复习的复习组
	private int status;// 完成第几次复习/强化

	private WordGroupBean mGroupBean;
	private List<WordBean> mWordBean;
	private ReviewFinishAdapter mAdapter;
	private int lastItem = -1;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		ViewUtils.inject(this);

		mContext = this;
		mWordBean = new ArrayList<WordBean>();
		mGroups = new ArrayList<WordGroup>();
		mReviewGroups = new ArrayList<WordGroup>();
		init();
	}

	@Override
	public void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		MobclickAgent.onPageStart(UmengPages.PAGE_REVIEW_FINISH);
	}

	@Override
	public void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		MobclickAgent.onPageEnd(UmengPages.PAGE_REVIEW_FINISH);
	}

	private void init() {
		// next_learn = (Button) view.findViewById(R.id.next_learn);
		backBtn.setOnClickListener(this);
		int review_finish_bg = PreferencesUtil.getInt(mContext, "review_bg", 0);
		int[] images = YsApplication.getImgs();
		review_finish_rl.setBackgroundResource(images[review_finish_bg]);
		// next_learn.setOnClickListener(this);

		isFinish = getIntent().getBooleanExtra("isFinish", false);
		YsApplication myApplication = (YsApplication) getApplication();
		mGroupBean = myApplication.getmGroupBean();
		mWordBean = mGroupBean.getWords();
		mAdapter = new ReviewFinishAdapter(mContext);
		mAdapter.setmList(mWordBean);
		review_list.setAdapter(mAdapter);

		if (6 == Integer.parseInt(mGroupBean.getStatus()) - 1) {
			choose_review.setText(R.string.choose_review_finish);
		} else {
			choose_review.setText(R.string.review_finish);
		}

		wgid = PreferencesUtil.getInt(mContext, "nextWgid", 1);
		status = PreferencesUtil.getInt(mContext, "status", 1);
		isReview = PreferencesUtil.getInt(mContext, "isReview", 0);
		String mGroupBeanStr = PreferencesUtil.getString(mContext, "mGroupBeanStr", "");
		mReviewGroupBean = JSON.parseObject(mGroupBeanStr, ReviewGroupBean.class);
		mGroups = mReviewGroupBean.getW_groups();
		PreferencesUtil.putString(mContext, "temp_status", "2");
		if (mWordBean != null) {
			PreferencesUtil.putInt(mContext, "finish_num", mWordBean.size());
		}
		review_title.setText(R.string.strengthen_title);
		review_list.setOnGroupExpandListener(new OnGroupExpandListener() {

			@Override
			public void onGroupExpand(int groupPosition) {
				if (lastItem >= 0 && lastItem != groupPosition) {
					review_list.collapseGroup(lastItem);
				}
				lastItem = groupPosition;
			}
		});
	}

	@Override
	public void onClickEvent(View view) {
		switch (view.getId()) {
		case R.id.navi_back_btn:
			finish();
			break;

		}
	}
}
